import {AbilityModel} from "../../controller/model/AbilityModel";
import Configer from "../../frame/apiModel/config/Configer";
import {FinalKey} from "../../constant/FinalConstant";
import {UnitController} from "../../controller/UnitController";
import {Logger} from "../../frame/apiModel/config/Logger";
import {UnitModel} from "../../frame/apiModel/adapterModel/model3/UnitModel";
import {ItemModel} from "../../frame/apiModel/adapterModel/model3/ItemModel";
import {PlayerModel} from "../../frame/apiModel/adapterModel/model4/PlayerModel";
import {LocModel} from "../../frame/apiModel/base/LocModel";
import {PlayerFactory} from "../../frame/PlayerFactory";
import {DataRouterUtil} from "../../utils/router/DataRouterUtil";
import {CodeUtil} from "../../frame/codeAdapter/CodeUtil";
import {AttributeEnum} from "../../frame/dataSource/DataModel";
import {Util} from "../../utils/Util";
import {AbilityUtil} from "./AbilityUtil";
import {GlobalEnv} from "../../utils/GlobalEnv";
import {UnitJson} from "../../frame/apiModel/model/StoreJson";
import {TimeControllerOpt} from "../../controller/model/ControllerOpt";
import {TimeController} from "../../controller/TimeController";

export default class PhoenixSkillModel extends AbilityModel {
    abilityId: string = Configer.getFinal(FinalKey.phoenixSkill) as string;

    actionFun = (activeUnit: UnitModel, activeItem: ItemModel, targetUnit: UnitModel, targetItem: ItemModel, activePlayer: PlayerModel, targetPlayer: PlayerModel, spellId: string, spellLoc: LocModel)  => {
        let success = true;
        this.phoenixSkill(activeUnit, activePlayer, spellLoc);
        return success;
    }

    phoenixSkill (activeUnit: UnitModel, player: PlayerModel, spellLoc: LocModel) {
        const id = Configer.getFinal(FinalKey.phoenixEgg) as string;
        // const unitLoc = GetUnitLoc(activeUnit);
        const unitJson: UnitJson = { id: id, e: 0 };
        const arr = UnitController.getInstance().createUnit(1, unitJson, player, spellLoc, 0);
        activeUnit.show = false;
        const delayCallBack = () => {
            for (let i = arr.length - 1; i >= 0; i--) {
                const unit = arr[i];
                UnitController.getInstance().unitDeadRemove(unit);
            }
            activeUnit.show = true;
            const maxLife = Util.getTargetAttributeEnumByNotHero(activeUnit, AttributeEnum.maxLife, true);
            Util.setTargetAttributeEnumToNotHero(activeUnit, AttributeEnum.life, maxLife);
        }
        const timerOpt = new TimeControllerOpt(undefined, delayCallBack, 1);
        const phoenixTime = Configer.getFinal(FinalKey.phoenixTime) as number
        TimeController.getInstance().startTimer({time: phoenixTime, opt: timerOpt});
    }
}